﻿using Company.Application.Share.Events;
using Company.Application.Share.Prism;
using Company.Application.Share.Session;
using Company.Core.IOC;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;
using ReactiveUI;
using ReactiveUI.Fody.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace Shell.ViewModels
{
    public class MainWindowViewModel : ReactiveObject
    {
        public MainWindowViewModel()
        {
            LoadedCommand = new DelegateCommand(OnLoaded);
            UnloadedCommand = new DelegateCommand(OnUnloaded);

            // 订阅登录成功消息
            PrismProvider.EventAggregator.GetEvent<LoginSuccessedEvent>().Unsubscribe(OnLogin);
            PrismProvider.EventAggregator.GetEvent<LoginSuccessedEvent>().Subscribe(OnLogin, ThreadOption.UIThread);
        }
        public bool HardwareInitialized { get; set; }
        public ICommand LoadedCommand { get; }
        public ICommand UnloadedCommand { get; }
        private void OnLoaded() { }
        private void OnUnloaded() { }


        private void OnLogin(CurrentUser user)
        {
            if (HardwareInitialized)
            {
                // 加载Main模块
                PrismProvider.ModuleManager.LoadModule(ModuleNames.MainModule);
                PrismProvider.RegionManager.RequestNavigate(RegionNames.MainRegion, ViewNames.MainView);
                PrismProvider.MainWindow.WindowState = System.Windows.WindowState.Maximized;
            }
            else
            {
                // 加载硬件模块
                PrismProvider.ModuleManager.LoadModule(ModuleNames.HardwareModule);
                PrismProvider.RegionManager.RequestNavigate(RegionNames.MainRegion, ViewNames.HardwareInitView);
            }
        }
    }
}
